﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace Data
{
    public class CategoryData:DataBase
    {
        public List<Category> GetCategories(int id = 0 ,string name = null, string orderBy = null)
        {
            var dataTable = ExecuteDataTable("GetCategories", new List<SqlParameter>
                                                                  {
                                                                      new SqlParameter("@Id", id),
                                                                      new SqlParameter("@Name", name),
                                                                      new SqlParameter("@OrderBy", orderBy )
                                                                  });
            if (dataTable.Rows.Count == 0) return null;

            var categoryList = new List<Category>();

            foreach (DataRow row in dataTable.Rows)
            {
                categoryList.Add(new Category
                                     {
                                         Id = Convert.ToInt32(row["Id"]),
                                         Name = Convert.ToString(row["Name"]),
                                         Description = Convert.ToString(row["Description"])
                                     });
            }
            return categoryList;
        }

        public void AddCategory(Category category)
        {

            ExecuteNonQuery("AddCategory", new List<SqlParameter>
                                                 {
                                                     new SqlParameter("@Name", category.Name),
                                                     new SqlParameter("@Description", category.Description)
                                                 });

        }

        public void DeleteCategory(int id)
        {
           ExecuteNonQuery("DeleteCategory",new List<SqlParameter>{ new SqlParameter("@Id",id) });
        }

        public void UpdateCategory(Category category)
        {
            ExecuteNonQuery("UpdateCategory", new List<SqlParameter>
                                                 {
                                                     new SqlParameter("@Id", category.Id),
                                                     new SqlParameter("@Name", category.Name),
                                                     new SqlParameter("@Description", category.Description)
                                                 });
        }
    }
}